package br.com.visagio.aulabigdata.util;

import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.visagio.aulabigdata.business.TwitterBusiness;
import br.com.visagio.aulabigdata.enums.DB;

public class SolrUtil {
	
	Logger logger = LoggerFactory.getLogger(SolrUtil.class);
	
	private static String DB_HOST     = "http://localhost:8888/solr";
	
	public static final String INDEX = "";
	
	private HttpSolrServer server = null;
	
	private static SolrUtil instance = new SolrUtil();
	
	public static SolrUtil getInstance(){
		if(instance.getServer() == null){
			HttpSolrServer server = new HttpSolrServer(DB_HOST);
			instance.setServer(server);
		}
		return instance;
	}

	public void removeAllTweets() {
		try {
			server.deleteByQuery("*:*");
			server.commit();
			logger.info("Todos os dados do Solr foram removidos");
		} catch (Exception e) {
			throw new RuntimeException("Erro ao tentar remover dados do banco");
		}
	}

	public HttpSolrServer getServer() {
		return server;
	}

	public void setServer(HttpSolrServer server) {
		this.server = server;
	}
	
	/**
	 * Testa conexão com host
	 * @throws IOException 
	 * @throws SolrServerException 
	 */
	public static void main(String[] args) throws SolrServerException, IOException {
		HttpSolrServer server = getInstance().getServer();
		
		instance.logger.info("Conectado ao servidor " + server.getBaseURL());
		
		instance.removeAllTweets();
		
	}
	


}
